package com.example.huajia.wx.controller;

import com.example.huajia.wx.common.util.R;
import com.example.huajia.wx.common.util.SnowFlakeGenerateIdWorker;
import com.example.huajia.wx.config.shiro.JwtUtil;
import com.example.huajia.wx.controller.form.DeleteAddressInfoForm;
import com.example.huajia.wx.controller.form.InsertAddressInfoForm;
import com.example.huajia.wx.controller.form.UpdateAddressInfoForm;
import com.example.huajia.wx.exception.HJException;
import com.example.huajia.wx.service.InfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/info")
@Api("用户下载地址及其信息接口")
public class InfoController {
    @Resource
    private InfoService infoService;
    @Resource
    private JwtUtil jwtUtil;

    @GetMapping("/searchUserAddressInfo")
    @ApiOperation("查询用户下单地址")
    public R searchUserAddressInfo(@RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        ArrayList<HashMap> list = infoService.searchUserAddressInfo(userId);
        return R.ok().put("result", list);
    }

    @PostMapping("/insertAddressInfo")
    @ApiOperation("用户新增收货地址")
    public R insertAddressInfo(@Valid @RequestBody InsertAddressInfoForm form, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        SnowFlakeGenerateIdWorker snowFlakeGenerateIdWorker = new SnowFlakeGenerateIdWorker(0L, 0L);
        String id = snowFlakeGenerateIdWorker.generateNextId();
        String address = form.getAddress();
        String name = form.getName();
        String phone = form.getPhone();
        HashMap map = new HashMap();
        map.put("userId", userId);
        map.put("address", address);
        map.put("name", name);
        map.put("phone", phone);
        map.put("id", id);
        int row = infoService.insertAddressInfo(map);
        if (row == 0){
            throw new HJException("添加收货地址失败！");
        }
        return R.ok();
    }

    @PostMapping("/updateAddressInfo")
    @ApiOperation("用户修改收货地址")
    public R updateAddressInfo(@Valid @RequestBody UpdateAddressInfoForm form, @RequestHeader("token") String token){
        String id = form.getId();
        String address = form.getAddress();
        String name = form.getName();
        String phone = form.getPhone();
        HashMap map = new HashMap();
        map.put("id", id);
        map.put("address", address);
        map.put("name", name);
        map.put("phone", phone);
        int row = infoService.updateAddressInfo(map);
        if (row == 0){
            throw new HJException("修改收货地址失败！");
        }
        return R.ok();
    }

    @PostMapping("/deleteAddressInfo")
    @ApiOperation("删除用户下单地址")
    public R deleteAddressInfo(@Valid@RequestBody DeleteAddressInfoForm form, @RequestHeader("token") String token){
        int userId = jwtUtil.getUserId(token);
        String id = form.getId();
        int row = infoService.deleteAddressInfo(id);
        if (row == 0){
            throw new HJException("删除收货地址失败！");
        }
        return R.ok();
    }
}
